﻿var PSS_Menu = {
	init : function(){
		PSS_Menu.loadMenuData();
	},
	pssMenus : undefined,
	loadMenuData : function(){
		if(!PSS_Menu.pssMenus){
			$.ajax({
				type: "GET",
				url: "menus",
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: function(data) {
					PSS_Menu.pssMenus = data;
					if(!PSS_Menu.pssMenus){
						alert("网络错误");
					}else{
						PSS_Menu.initLeftMenu();
						PSS_Menu.tabClose();
						PSS_Menu.tabCloseEven();
						PSS_Menu.initTab();
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					alert("网络错误");
				}
			});
		}
	},
	initTab : function(){
		$('#tabs').tabs('add',{
			title:'XX 系统平台',
			content:PSS_Menu.createFrame('')
		}).tabs({
	        onSelect: function (title) {
	            var currTab = $('#tabs').tabs('getTab', title);
	            var iframe = $(currTab.panel('options').content);

				var src = iframe.attr('src');
				if(src)
					$('#tabs').tabs('update', { 
						tab: currTab, 
						options: { 
							content: PSS_Menu.createFrame(src)
						} 
					});

	        }
	    });
	},
	initLeftMenu : function(){
		$("#nav").accordion({
			animate:false
		});
		$.each(PSS_Menu.pssMenus.menus, function(i, n) {
			var menulist ='';
			menulist +='<ul>';
	        $.each(n.menus, function(j, o) {
				menulist += '<li><div><a ref="'+o.menuName+'" href="#" rel="' + o.url + '" ><span class="icon '+o.icon+'" >&nbsp;</span><span class="nav">' + o.menuName + '</span></a></div></li> ';
	        });
			menulist += '</ul>';
			$('#nav').accordion('add', {
	            title: n.menuName,
	            content: menulist,
	            iconCls: 'icon ' + n.icon
	        });
	    });
		$('.easyui-accordion li a').click(function(){
			var tabTitle = $(this).children('.nav').text();
			var url = $(this).attr("rel");
			var menuId = $(this).attr("ref");
			var icon = PSS_Menu.getIcon(menuId,icon);
			PSS_Menu.addTab(tabTitle,url,icon);
			$('.easyui-accordion li div').removeClass("selected");
			$(this).parent().addClass("selected");
		}).hover(function(){
			$(this).parent().addClass("hover");
		},function(){
			$(this).parent().removeClass("hover");
		});
		//选中第一个
		var panels = $('#nav').accordion('panels');
		var t = panels[0].panel('options').title;
	    $('#nav').accordion('select', t);
	},
	getIcon : function(menuId){
		var icon = 'icon ';
		$.each(PSS_Menu.pssMenus.menus, function(i, n) {
			 $.each(n.menus, function(j, o) {
			 	if(o.menuId==menuId){
					icon += o.icon;
				}
			 });
		});
		return icon;
	},
	addTab : function(subtitle,url,icon){
		if(!$('#tabs').tabs('exists',subtitle)){
			$('#tabs').tabs('add',{
				title:subtitle,
				content:PSS_Menu.createFrame(url),
				closable:true,
				icon:icon
			});
		}else{
			$('#tabs').tabs('select',subtitle);
			$('#mm-tabupdate').click();
		}
		PSS_Menu.tabClose();
	},
	createFrame : function(url){
		var s = '<iframe scrolling="auto" frameborder="0"  src="'+url+'" style="width:100%;height:100%;"></iframe>';
		return s;
	},
	tabClose : function(){
		/*双击关闭TAB选项卡*/
		$(".tabs-inner").dblclick(function(){
			var subtitle = $(this).children(".tabs-closable").text();
			$('#tabs').tabs('close',subtitle);
		});
		/*为选项卡绑定右键*/
		$(".tabs-inner").bind('contextmenu',function(e){
			$('#mm').menu('show', {
				left: e.pageX,
				top: e.pageY
			});
			var subtitle =$(this).children(".tabs-closable").text();
			$('#mm').data("currtab",subtitle);
			$('#tabs').tabs('select',subtitle);
			return false;
		});
	},
	tabCloseEven : function(){
		//刷新
		$('#mm-tabupdate').click(function(){
			var currTab = $('#tabs').tabs('getSelected');
			var url = $(currTab.panel('options').content).attr('src');
			$('#tabs').tabs('update',{
				tab:currTab,
				options:{
					content:PSS_Menu.createFrame(url)
				}
			});
		});
		//关闭当前
		$('#mm-tabclose').click(function(){
			var currtab_title = $('#mm').data("currtab");
			$('#tabs').tabs('close',currtab_title);
		});
		//全部关闭
		$('#mm-tabcloseall').click(function(){
			$('.tabs-inner span').each(function(i,n){
				var t = $(n).text();
				$('#tabs').tabs('close',t);
			});
		});
		//关闭除当前之外的TAB
		$('#mm-tabcloseother').click(function(){
			$('#mm-tabcloseright').click();
			$('#mm-tabcloseleft').click();
		});
		//关闭当前右侧的TAB
		$('#mm-tabcloseright').click(function(){
			var nextall = $('.tabs-selected').nextAll();
			if(nextall.length==0){
				//msgShow('系统提示','后边没有啦~~','error');
				alert('后边没有啦~~');
				return false;
			}
			nextall.each(function(i,n){
				var t=$('a:eq(0) span',$(n)).text();
				$('#tabs').tabs('close',t);
			});
			return false;
		});
		//关闭当前左侧的TAB
		$('#mm-tabcloseleft').click(function(){
			var prevall = $('.tabs-selected').prevAll();
			if(prevall.length==0){
				alert('到头了，前边没有啦~~');
				return false;
			}
			prevall.each(function(i,n){
				var t=$('a:eq(0) span',$(n)).text();
				$('#tabs').tabs('close',t);
			});
			return false;
		});
		//退出
		$("#mm-exit").click(function(){
			$('#mm').menu('hide');
		});
	},
	msgShow : function(title, msgString, msgType){
		$.messager.alert(title, msgString, msgType);
	}
};